package com.bwie.auth.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.bwie.auth.reomte.AuthReomte;
import com.bwie.auth.service.AuthServicee;
import com.bwie.auth.utils.SendSmsUtils;
import com.bwie.common.constants.JwtConstants;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.User;
import com.bwie.common.domain.UserDto;
import com.bwie.common.result.Result;
import com.bwie.common.utils.JwtUtils;
import com.bwie.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceeImpl implements AuthServicee {


    @Autowired
    AuthReomte authReomte;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    SendSmsUtils sendSmsUtils;
    @Override
    public void send(User user) {
        if (StringUtils.isEmpty(user.getPhone())){
            throw  new RuntimeException("手机号不能为空");
        }
        if (!user.getPhone().matches("^1[3-9]\\d{9}$")){
            throw  new RuntimeException("手机号的格式不对");
        }
        Result<User> selectt = authReomte.selectt(user.getPhone());
        User data = selectt.getData();
        if (data==null){
            throw  new RuntimeException("改手机号不存在");
        }
        String code = RandomUtil.randomNumbers(4);
            sendSmsUtils.sendSms(user.getPhone(),JSONObject.toJSONString(new HashMap<String,String>(){{
                put("code",code);
            }}));

            redisTemplate.opsForValue().set(user.getPhone(),code,5,TimeUnit.MINUTES);
    }

    @Override
    public UserDto loog(User user) {
        if (StringUtils.isEmpty(user.getPhone())){
            throw  new RuntimeException("手机号不能为空");
        }
        if (!user.getPhone().matches("^1[3-9]\\d{9}$")){
            throw  new RuntimeException("手机号的格式不对");
        }
        Result<User> selectt = authReomte.selectt(user.getPhone());
        User data = selectt.getData();
        if (data==null){
            throw  new RuntimeException("该用户不存在");
        }
//        User u = (User) redisTemplate.opsForValue().get(data.getPhone());
//        if (u==null){
//            throw  new RuntimeException("验证码没有发送");
//        }
//        if (!ObjectUtil.equal(u,user.getCode())){
//            throw  new RuntimeException("验证码不对");
//        }
        Map<String, Object> map = new HashMap<>();
        String userKey = IdUtil.fastSimpleUUID();
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(data),20, TimeUnit.MINUTES);
        UserDto userDto = new UserDto();
        userDto.setToken(token);
        userDto.setTime("30");
        return userDto;
    }


}
